package org.onvivals.admin.module.pms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.pms.entity.PmsLlmConfig;
import org.onvivals.module.pms.param.PmsLlmConfigParam;
import org.onvivals.module.pms.service.IPmsLlmConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "大模型管理")
@RestController
@RequestMapping("/pms/llm")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PmsLlmConfigController {

    private final IPmsLlmConfigService pmsLlmConfigService;

    @Operation(summary = "分页查询全部大模型")
    @GetMapping("/page")
    public Result<PageResult<PmsLlmConfig>> page(PmsLlmConfigParam param) {
        PageParam<PmsLlmConfig, PmsLlmConfigParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
        IPage<PmsLlmConfig> pa = pmsLlmConfigService.page(page, page.getWrapper());
        PageResult<PmsLlmConfig> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
        return Result.OK("查询成功", pageResult);
    }

    @Operation(summary = "查询全部大模型")
    @GetMapping()
    public Result<List<PmsLlmConfig>> list(PmsLlmConfigParam param) {
        PageParam<PmsLlmConfig, PmsLlmConfigParam> page = new PageParam<>(param);
        List<PmsLlmConfig> pmsLlmConfigList = pmsLlmConfigService.list(page.getOrderWrapper());
        return Result.OK("查询成功", pmsLlmConfigList);
    }

    @Operation(summary = "根据id查询大模型")
    @GetMapping("/{id}")
    public Result<PmsLlmConfig> get(@PathVariable("id") Long id) {
        PmsLlmConfig pmsLlmConfig = pmsLlmConfigService.getById(id);
        return Result.OK("查询成功", pmsLlmConfig);
    }

    @Operation(summary = "添加大模型")
    @PostMapping()
    public Result<?> save(@RequestBody PmsLlmConfig pmsLlmConfig) {
        if (pmsLlmConfigService.save(pmsLlmConfig)) {
            return Result.OK("添加成功");
        }
        return Result.error("添加失败");
    }

    @Operation(summary = "修改大模型")
    @PutMapping()
    public Result<?> update(@RequestBody PmsLlmConfig pmsLlmConfig) {
        if (pmsLlmConfigService.updateById(pmsLlmConfig)) {
            return Result.OK("修改成功");
        }
        return Result.error("修改失败");
    }

    @Operation(summary = "删除大模型")
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) {
        if (pmsLlmConfigService.removeById(id)) {
            return Result.OK("删除成功");
        }
        return Result.error("删除失败");
    }

}
